﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YJKRevitBase.WinForm;
using YJKRevitFamilyDataBase;
using YJKInspectModel.ModelValidation.Utils;
using YJKInspectModel.ModelValidation.Walls;

namespace YJKInspectModel.ModelValidation.FireCompartment
{
	public partial class DlgFireOutlineWinDoorWallCheck : System.Windows.Forms.Form
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void CancelRevitOperate()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgFireOutlineWinDoorWallCheck.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgFireOutlineWinDoorWallCheck.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void SetFocusToRevit()
		{
			int wMsg = 7;
			DlgFireOutlineWinDoorWallCheck.SendMessage(this.m_wndHandleRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private void DlgFireOutlineWinDoorWallCheck_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SetFocusToRevit();
			DlgFireOutlineWinDoorWallCheck.g_Dlg_X = base.Location.X;
			DlgFireOutlineWinDoorWallCheck.g_Dlg_Y = base.Location.Y;
		}

		private void DlgFireOutlineWinDoorWallCheck_Load(object sender, EventArgs e)
		{
			base.Location = new System.Drawing.Point(DlgFireOutlineWinDoorWallCheck.g_Dlg_X, DlgFireOutlineWinDoorWallCheck.g_Dlg_Y);
			this.chbZoomToElement.Checked = true;
			this.InitFamilyLibDoorWins();
		}

		public DlgFireOutlineWinDoorWallCheck()
		{
			this.InitializeComponent();
		}

        public DlgFireOutlineWinDoorWallCheck(ExternalCommandData cmdData, List<ViewPlan> lstView, WindowHandle hHandle)
		{
			this.InitializeComponent();
			this.m_Revit = cmdData;
			this.m_lstViewPlan = lstView;
			this.m_wndHandleRevit = hHandle;
			this.m_UIDoc = this.m_Revit.Application.ActiveUIDocument;
			this.GetOnOutlineWinDoorWall();
		}

		public DlgFireOutlineWinDoorWallCheck(ExternalCommandData cmdData, ModelessFromRequestHandler handler, ExternalEvent exEvent, List<ViewPlan> lstView)
		{
			this.InitializeComponent();
			this.m_Revit = cmdData;
			this.m_ExEvent = exEvent;
			this.m_Handler = handler;
			this.m_lstViewPlan = lstView;
			this.m_UIDoc = this.m_Revit.Application.ActiveUIDocument;
			this.GetOnOutlineWinDoorWall();
		}

		public DlgFireOutlineWinDoorWallCheck(ExternalCommandData cmdData)
		{
			this.InitializeComponent();
			this.m_Revit = cmdData;
			this.m_UIDoc = this.m_Revit.Application.ActiveUIDocument;
		}

		private void buttonFireElement_Click(object sender, EventArgs e)
		{
			DlgFireOutlineWinDoorWallFamilyReplace dlgFireOutlineWinDoorWallFamilyReplace = new DlgFireOutlineWinDoorWallFamilyReplace(this.m_Revit);
			dlgFireOutlineWinDoorWallFamilyReplace.m_lstFamilyLibDoors = this.m_lstFamilyLibDoors;
			dlgFireOutlineWinDoorWallFamilyReplace.m_lstFamilyLibDoorsRolling = this.m_lstFamilyLibDoorsRolling;
			dlgFireOutlineWinDoorWallFamilyReplace.m_lstFamilyLibWins = this.m_lstFamilyLibWins;
			if (dlgFireOutlineWinDoorWallFamilyReplace.ShowDialog() == DialogResult.OK)
			{
				this.m_strDoorClassAFiName = dlgFireOutlineWinDoorWallFamilyReplace.FireDoorClassAFiName;
				this.m_strDoorSuperFiName = dlgFireOutlineWinDoorWallFamilyReplace.FireDoorSuperFiName;
				this.m_strWindowFiName = dlgFireOutlineWinDoorWallFamilyReplace.FireWindowClassAFiName;
				this.MakeRequest(RequestId.ReplaceFamilyInstance);
			}
		}

		private void MakeRequest(RequestId request)
		{
			this.m_Handler.Request.Make(request);
			this.m_ExEvent.Raise();
		}

		public bool ReplaceFamilyInstance()
		{
			try
			{
				Document document = this.m_Revit.Application.ActiveUIDocument.Document;
				List<Family> list = new FilteredElementCollector(document).OfClass(typeof(Family)).Cast<Family>().ToList<Family>();
				foreach (DlgFireOutlineWinDoorWallCheck.Def_Sel def_Sel in this.m_lstDefine)
				{
					Family family = null;
					switch (def_Sel.m_kType)
					{
					case DlgFireOutlineWinDoorWallCheck.EleType.eDoor:
						if (!string.IsNullOrEmpty(this.m_strDoorClassAFiName))
						{
							family = list.Find((Family Fi) => Fi.Name == this.m_strDoorClassAFiName);
							if (family == null)
							{
								DbFamily dbFamily = this.m_lstFamilyLibDoors.Find((DbFamily e) => e.Name == this.m_strDoorClassAFiName);
								if (dbFamily != null)
								{
									family = dbFamily.Load(document);
									if (family != null)
									{
										list.Add(family);
									}
								}
							}
						}
						break;
					case DlgFireOutlineWinDoorWallCheck.EleType.eWindow:
						if (!string.IsNullOrEmpty(this.m_strWindowFiName))
						{
							family = list.Find((Family Fi) => Fi.Name == this.m_strWindowFiName);
							if (family == null)
							{
								DbFamily dbFamily2 = this.m_lstFamilyLibWins.Find((DbFamily e) => e.Name == this.m_strWindowFiName);
								if (dbFamily2 != null)
								{
									family = dbFamily2.Load(document);
									if (family != null)
									{
										list.Add(family);
									}
								}
							}
						}
						break;
					case DlgFireOutlineWinDoorWallCheck.EleType.eDoorRolling:
						if (!string.IsNullOrEmpty(this.m_strDoorSuperFiName))
						{
							family = list.Find((Family Fi) => Fi.Name == this.m_strDoorSuperFiName);
							if (family == null)
							{
								DbFamily dbFamily3 = this.m_lstFamilyLibDoors.Find((DbFamily e) => e.Name == this.m_strDoorSuperFiName);
								if (dbFamily3 != null)
								{
									family = dbFamily3.Load(document);
									if (family != null)
									{
										list.Add(family);
									}
								}
							}
						}
						break;
					}
					if (family != null)
					{
						if (def_Sel.m_kType == DlgFireOutlineWinDoorWallCheck.EleType.eDoor)
						{
							bool flag = def_Sel.IsSingleDoor();
							double num = AssistFunc.feetToMM(family.GetSymbols().FirstOrDefault<FamilySymbol>().GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsDouble());
							if (flag != DlgFireOutlineWinDoorWallCheck.Def_Sel.IsSingleDoor((int)num))
							{
								continue;
							}
						}
						string strSymbolName = def_Sel.GetNewSymbolName();
						FamilyInstance familyInstance = def_Sel.m_element as FamilyInstance;
						FamilySymbol familySymbol = family.GetSymbols().FirstOrDefault((FamilySymbol fiSymbol) => fiSymbol.Name == strSymbolName);
						if (familySymbol == null)
						{
							Transaction transaction = new Transaction(document, "DuplicateFiSymbol");
							transaction.Start();
							try
							{
								familySymbol = (family.GetSymbols().FirstOrDefault<FamilySymbol>().Duplicate(strSymbolName) as FamilySymbol);
								Parameter parameter = familyInstance.Symbol.GetParameter(BuiltInParameter.FAMILY_HEIGHT_PARAM);
								if (parameter != null)
								{
									double num2 = parameter.AsDouble();
									familySymbol.GetParameter(BuiltInParameter.FAMILY_HEIGHT_PARAM).Set(num2);
								}
								parameter = familyInstance.Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH);
								if (parameter != null)
								{
									double num3 = parameter.AsDouble();
									familySymbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).Set(num3);
								}
								transaction.Commit();
							}
							catch (Exception)
							{
								transaction.RollBack();
							}
						}
						Transaction transaction2 = new Transaction(document, "ReplaceFi");
						transaction2.Start();
						try
						{
							familyInstance.Symbol = familySymbol;
							transaction2.Commit();
						}
						catch
						{
							transaction2.RollBack();
						}
					}
				}
			}
			catch
			{
				return false;
			}
			return true;
		}

		public bool CloseFireAreaWalls()
		{
			ViewPlan activeView = this.m_Revit.Application.ActiveUIDocument.ActiveView as ViewPlan;
			foreach (ViewPlan viewPlan in this.m_lstViewPlan)
			{
				foreach (Area area in this.GetAllAreaByView(viewPlan.Id))
				{
					List<Curve> list = new List<Curve>();
					foreach (IList<BoundarySegment> list2 in area.GetBoundarySegments(new SpatialElementBoundaryOptions()))
					{
						foreach (BoundarySegment segment in list2)
						{
							list.Add(segment.GetCurve());
						}
					}
					string name = area.Name;
					List<Wall> allWallByView = this.GetAllWallByView(viewPlan.Id);
					List<Wall> list3 = new List<Wall>();
					int num = 0;
					foreach (Wall wall in allWallByView)
					{
						Curve curve = (wall.Location as LocationCurve).Curve;
						curve.GetEndPoint(0);
						curve.GetEndPoint(1);
						num++;
						int num2 = 0;
						foreach (Curve curve2 in list)
						{
							num2++;
							curve2.GetEndPoint(0);
							curve2.GetEndPoint(1);
							if (this.IsCurveCrossWall(curve2, wall) && list3.IndexOf(wall) < 0)
							{
								list3.Add(wall);
							}
						}
					}
					double val = 3000.0;
					if (list3.Count > 0)
					{
						this.m_Revit.Application.ActiveUIDocument.ActiveView = viewPlan;
						WallsValidationManager wallsValidationManager = new WallsValidationManager(this.m_Revit);
						List<ElementId> list4 = new List<ElementId>();
						wallsValidationManager.WallClosedValidation(list3, AssistFunc.mmToFeet(val), ref list4);
					}
				}
			}
			this.m_Revit.Application.ActiveUIDocument.ActiveView = activeView;
			return true;
		}

		public bool InitFamilyLibDoorWins()
		{
			try
			{
				SqliteDatabase sqliteDB = FamilyLibDatabase.GetSqliteDB(SubjectType.Architecture, this.m_Revit.Application.Application.VersionNumber);
				DbCategory dbCategory = sqliteDB.CategoryTable["防火门"];
				this.m_lstFamilyLibDoors = dbCategory.GetAllFamilies(true, CheckType.All);
				DbCategory dbCategory2 = sqliteDB.CategoryTable["防火卷帘门"];
				this.m_lstFamilyLibDoorsRolling = dbCategory2.GetAllFamilies(true, CheckType.All);
				DbCategory dbCategory3 = sqliteDB.CategoryTable["防火窗"];
				this.m_lstFamilyLibWins = dbCategory3.GetAllFamilies(true, CheckType.All);
			}
			catch
			{
				return false;
			}
			return true;
		}

		private void buttonLinkFireWall_Click(object sender, EventArgs e)
		{
			this.MakeRequest(RequestId.CloseFireAreaWalls);
		}

		private void DlgFireOutlineWinDoorWallCheck_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SetFocusToRevit();
				return;
			}
			base.Focus();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.SetFocusToRevit();
			this.CancelRevitOperate();
			base.Close();
		}

		private void ZoomViewSelect(ListView lstView)
		{
			if (lstView.SelectedItems.Count != 1)
			{
				return;
			}
			int index = lstView.SelectedItems[0].Index;
			if (lstView.Items[index].SubItems.Count == 1)
			{
				return;
			}
			DlgFireOutlineWinDoorWallCheck.Def_Sel def_Sel = lstView.Items[index].SubItems[1].Tag as DlgFireOutlineWinDoorWallCheck.Def_Sel;
			string strEleName = def_Sel.m_strEleName;
			if (this.m_UIDoc.ActiveView.Id.IntegerValue != def_Sel.m_viewId.IntegerValue)
			{
				this.m_UIDoc.ActiveView = (this.m_UIDoc.GetElementById(def_Sel.m_viewId) as ViewPlan);
			}
			Element element = def_Sel.m_element;
			if (this.chbZoomToElement.Checked)
			{
				this.m_UIDoc.ShowElements(element);
			}
			List<Element> list = new List<Element>();
			list.Add(element);
			RevitVersionFuncs.SetSelection(this.m_Revit.Application.ActiveUIDocument.Selection, list);
			this.m_Revit.Application.ActiveUIDocument.RefreshActiveView();
		}

		private void listViewWall_DoubleClick(object sender, EventArgs e)
		{
			this.ZoomViewSelect(this.listViewWall);
		}

		private void listViewWinDoor_DoubleClick(object sender, EventArgs e)
		{
			this.ZoomViewSelect(this.listViewWinDoor);
		}

		private void buttonRenew_Click(object sender, EventArgs e)
		{
			this.GetOnOutlineWinDoorWall();
		}

		private List<Area> GetAllAreaByView(ElementId viewId)
		{
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			List<Area> list = new List<Area>();
			foreach (Element element in new FilteredElementCollector(document, viewId).OfCategory(BuiltInCategory.OST_Areas))
			{
				Area area = element as Area;
				if (area != null)
				{
					list.Add(area);
				}
			}
			return list;
		}

		public void AddDoorWinDefine(ViewPlan view, Element eleDoorWin)
		{
			DlgFireOutlineWinDoorWallCheck.Def_Sel define = this.GetDefine(view, eleDoorWin);
			this.m_lstDefine.Add(define);
		}

		private DlgFireOutlineWinDoorWallCheck.Def_Sel GetDefine(ViewPlan view, Element ele)
		{
			DlgFireOutlineWinDoorWallCheck.Def_Sel def_Sel = new DlgFireOutlineWinDoorWallCheck.Def_Sel();
			def_Sel.m_viewId = view.Id;
			def_Sel.m_strViewName = RevitVersionFuncs.GetViewName(view);
			def_Sel.m_element = ele;
			FamilyInstance familyInstance = ele as FamilyInstance;
			string name;
			string text;
			if (familyInstance != null)
			{
				name = familyInstance.Symbol.Name;
				text = familyInstance.Symbol.Family.Name;
			}
			else
			{
				name = ele.Name;
				text = name;
			}
            BuiltInCategory integerValue = (BuiltInCategory)ele.Category.Id.IntegerValue;
			string strEleName;
            if ((int)integerValue == -2000014)
			{
				def_Sel.m_kType = DlgFireOutlineWinDoorWallCheck.EleType.eWindow;
				strEleName = text + " " + name;
			}
            else if ((int)integerValue == -2000011)
			{
				def_Sel.m_kType = DlgFireOutlineWinDoorWallCheck.EleType.eWall;
				strEleName = text;
			}
			else
			{
				strEleName = text + " " + name;
				if (text.IndexOf("卷帘") >= 0)
				{
					def_Sel.m_kType = DlgFireOutlineWinDoorWallCheck.EleType.eDoorRolling;
				}
				else
				{
					def_Sel.m_kType = DlgFireOutlineWinDoorWallCheck.EleType.eDoor;
				}
			}
			def_Sel.m_strEleName = strEleName;
			return def_Sel;
		}

		private void GetOnOutlineWinDoorWall()
		{
			this.m_lstDefine.Clear();
			foreach (ViewPlan viewPlan in this.m_lstViewPlan)
			{
				List<Area> allAreaByView = this.GetAllAreaByView(viewPlan.Id);
				if (allAreaByView.Count != 0)
				{
					List<Curve> crossSideSegFromAreas = this.GetCrossSideSegFromAreas(allAreaByView);
					if (crossSideSegFromAreas.Count != 0)
					{
						List<Wall> allWallByView = this.GetAllWallByView(viewPlan.Id);
						List<Wall> list = new List<Wall>();
						foreach (Wall wall in allWallByView)
						{
							foreach (Curve curve in crossSideSegFromAreas)
							{
								if (this.IsCurveCrossWall(curve, wall))
								{
									bool flag = false;
									using (List<Wall>.Enumerator enumerator4 = list.GetEnumerator())
									{
										while (enumerator4.MoveNext())
										{
											if (enumerator4.Current.Id == wall.Id)
											{
												flag = true;
												break;
											}
										}
									}
									if (!flag)
									{
										list.Add(wall);
									}
								}
							}
						}
						List<FamilyInstance> source = this.GetAllWinDoorByView(viewPlan.Id).Cast<FamilyInstance>().ToList<FamilyInstance>();
						using (List<Wall>.Enumerator enumerator2 = list.GetEnumerator())
						{
							while (enumerator2.MoveNext())
							{
								Wall ele = enumerator2.Current;
								DlgFireOutlineWinDoorWallCheck.Def_Sel define = this.GetDefine(viewPlan, ele);
								if (!UtilityTools.IsValid3HourFireWall(ele))
								{
									this.m_lstDefine.Add(define);
								}
								foreach (FamilyInstance familyInstance in from elem in source
								where elem.Host.Id == ele.Id
								select elem)
								{
									string name = familyInstance.Symbol.Family.Name;
									string name2 = familyInstance.Symbol.Name;
									if (!UtilityTools.IsValidDoorWinClassAFamilyName(name, name2))
									{
										XYZ xyz = (familyInstance.Location as LocationPoint).Point;
										xyz = new XYZ(xyz.X, xyz.Y, 0.0);
										bool flag2 = false;
										foreach (Curve curve2 in crossSideSegFromAreas)
										{
											XYZ xyz2 = curve2.Project(xyz).XYZPoint;
											xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
											if (Geometry.IsEqual(xyz, xyz2, 0.0001))
											{
												flag2 = true;
												break;
											}
										}
										if (flag2)
										{
											DlgFireOutlineWinDoorWallCheck.Def_Sel define2 = this.GetDefine(viewPlan, familyInstance);
											bool flag3 = false;
											foreach (DlgFireOutlineWinDoorWallCheck.Def_Sel def_Sel in this.m_lstDefine)
											{
												if (define2.m_element.Id == def_Sel.m_element.Id)
												{
													flag3 = true;
													break;
												}
											}
											if (!flag3)
											{
												this.m_lstDefine.Add(define2);
											}
										}
									}
								}
							}
						}
					}
				}
			}
			this.GetAllListWinDoor();
			this.GetAllListWall();
		}

		private List<Wall> GetAllWallByView(ElementId viewId)
		{
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			List<Wall> list = new List<Wall>();
			foreach (Element element in new FilteredElementCollector(document, viewId).OfCategory(BuiltInCategory.OST_Walls))
			{
				Wall wall = element as Wall;
				if (wall != null)
				{
					list.Add(wall);
				}
			}
			return list;
		}

		private List<Element> GetAllWinDoorByView(ElementId viewId)
		{
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			List<Element> list = new List<Element>();
			foreach (Element item in new FilteredElementCollector(document, viewId).OfCategory(BuiltInCategory.OST_Doors))
			{
				list.Add(item);
			}
			foreach (Element item2 in new FilteredElementCollector(document, viewId).OfCategory(BuiltInCategory.OST_Windows))
			{
				list.Add(item2);
			}
			return list;
		}

		private void GetAllListWinDoor()
		{
			this.listViewWinDoor.Clear();
			ColumnHeader columnHeader = new ColumnHeader();
			columnHeader.Text = "楼层";
			columnHeader.Width = 100;
			this.listViewWinDoor.Columns.Add(columnHeader);
			ColumnHeader columnHeader2 = new ColumnHeader();
			columnHeader2.Text = "名称";
			columnHeader2.Width = 200;
			this.listViewWinDoor.Columns.Add(columnHeader2);
			string text = "";
			foreach (DlgFireOutlineWinDoorWallCheck.Def_Sel def_Sel in this.m_lstDefine)
			{
				if (def_Sel.m_kType != DlgFireOutlineWinDoorWallCheck.EleType.eWall)
				{
					if (def_Sel.m_strViewName != text)
					{
						text = def_Sel.m_strViewName;
						ListViewItem value = new ListViewItem(text);
						this.listViewWinDoor.Items.Add(value);
					}
					ListViewItem listViewItem = new ListViewItem("");
					listViewItem.SubItems.Add(def_Sel.m_strEleName).Tag = def_Sel;
					this.listViewWinDoor.Items.Add(listViewItem);
				}
			}
		}

		private void GetAllListWall()
		{
			this.listViewWall.Clear();
			ColumnHeader columnHeader = new ColumnHeader();
			columnHeader.Text = "楼层";
			columnHeader.Width = 100;
			this.listViewWall.Columns.Add(columnHeader);
			ColumnHeader columnHeader2 = new ColumnHeader();
			columnHeader2.Text = "名称";
			columnHeader2.Width = 200;
			this.listViewWall.Columns.Add(columnHeader2);
			string text = "";
			foreach (DlgFireOutlineWinDoorWallCheck.Def_Sel def_Sel in this.m_lstDefine)
			{
				if (def_Sel.m_kType == DlgFireOutlineWinDoorWallCheck.EleType.eWall)
				{
					if (def_Sel.m_strViewName != text)
					{
						text = def_Sel.m_strViewName;
						ListViewItem value = new ListViewItem(text);
						this.listViewWall.Items.Add(value);
					}
					ListViewItem listViewItem = new ListViewItem("");
					listViewItem.SubItems.Add(def_Sel.m_strEleName).Tag = def_Sel;
					this.listViewWall.Items.Add(listViewItem);
				}
			}
		}

		private List<Curve> GetCrossSideSegFromAreas(List<Area> lstArea)
		{
			List<Curve> list = new List<Curve>();
			list.Clear();
			foreach (Area area in lstArea)
			{
				foreach (IList<BoundarySegment> list2 in area.GetBoundarySegments(new SpatialElementBoundaryOptions()))
				{
					foreach (BoundarySegment segment in list2)
					{
						list.Add(segment.GetCurve());
					}
				}
			}
			return this.GetCrossSideSegFromCurves(list);
		}

		private List<Curve> GetCrossSideSegFromCurves(List<Curve> lstTolCurves)
		{
			List<Curve> list = new List<Curve>();
			for (int i = 0; i < lstTolCurves.Count - 1; i++)
			{
				Curve curve = lstTolCurves[i];
				for (int j = i + 1; j < lstTolCurves.Count; j++)
				{
					Curve curve2 = lstTolCurves[j];
					Curve twoCurvesCrossSeg = this.GetTwoCurvesCrossSeg(curve, curve2);
					if (!(twoCurvesCrossSeg == null))
					{
						list.Add(twoCurvesCrossSeg);
					}
				}
			}
			return list;
		}

		private Curve GetTwoCurvesCrossSeg(Curve curve1, Curve curve2)
		{
			XYZ hyendPoint = curve1.GetEndPoint(0);
			XYZ hyendPoint2 = curve1.GetEndPoint(1);
			XYZ xyz = curve2.GetEndPoint(0);
			XYZ xyz2 = curve2.GetEndPoint(1);
			double num = 0.001;
			if (hyendPoint.DistanceTo(hyendPoint2) < num || xyz.DistanceTo(xyz2) < num)
			{
				return null;
			}
            if ((int)curve1.Intersect(curve2) != 64)
			{
				return null;
			}
			List<XYZ> list = new List<XYZ>();
			List<XYZ> list2 = new List<XYZ>();
			list.Add(hyendPoint);
			list.Add(hyendPoint2);
			list2.Add(xyz);
			list2.Add(xyz2);
			int num2 = 0;
			for (int i = 0; i < list.Count; i++)
			{
				XYZ xyz3 = list[i];
				for (int j = 0; j < list2.Count; j++)
				{
					XYZ xyz4 = list2[j];
					if (xyz3.IsAlmostEqualTo(xyz4, 0.001))
					{
						num2++;
					}
				}
			}
			if (num2 == 2)
			{
				return curve1.Clone();
			}
			if (num2 == 1)
			{
				if (curve1.Length < curve2.Length)
				{
					return curve1.Clone();
				}
				if (curve1.Length > curve2.Length)
				{
					return curve2.Clone();
				}
			}
			if (curve1 as Line != null)
			{
				List<XYZ> list3 = list;
				list3.Add(list2[0]);
				list3.Add(list2[1]);
				XYZ xyz5 = (hyendPoint2 - hyendPoint).Normalize();
				XYZ xyz6 = list3[0];
				List<double> list4 = new List<double>();
				list4.Add(0.0);
				for (int k = 1; k < list3.Count; k++)
				{
					XYZ xyz7 = list3[k] - xyz6;
					double num3 = xyz7.GetLength();
					if (!xyz7.Normalize().IsAlmostEqualTo(xyz5))
					{
						num3 *= -1.0;
					}
					list4.Add(num3);
				}
				list4.Sort();
				XYZ xyz8 = xyz6 + xyz5 * list4[1];
				XYZ xyz9 = xyz6 + xyz5 * list4[2];
				try
				{
					return Line.CreateBound(xyz8, xyz9);
				}
				catch (Exception ex)
				{
					string message = ex.Message;
					MessageBox.Show("GetTwoCurvesCrossSeg 创建line " + ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
					return null;
				}
			}
			Arc arc = curve1 as Arc;
			Arc arc2 = curve2 as Arc;
			if (arc == null)
			{
				return arc;
			}
			XYZ center = arc.Center;
			double radius = arc.Radius;
			XYZ normal = arc.Normal;
			XYZ normal2 = arc2.Normal;
			if (!normal.IsAlmostEqualTo(normal2))
			{
				XYZ xyz10 = xyz2;
				xyz2 = xyz;
				xyz = xyz10;
			}
			XYZ xyz11 = null;
			XYZ xyz12 = null;
			double num4 = arc.Distance(xyz2);
			double num5 = arc.Distance(xyz);
			if (num4 < num5)
			{
				xyz11 = xyz2;
			}
			else
			{
				xyz12 = xyz;
			}
			num4 = arc2.Distance(hyendPoint2);
			num5 = arc2.Distance(hyendPoint);
			if (num4 < num5)
			{
				xyz11 = hyendPoint2;
			}
			else
			{
				xyz12 = hyendPoint;
			}
			if (xyz12 == null || xyz11 == null)
			{
				return null;
			}
			double num6 = arc.XDirection.AngleOnPlaneTo(xyz11 - center, normal);
			double num7 = arc.XDirection.AngleOnPlaneTo(xyz12 - center, normal);
			if (num6 > Math.PI*2)
			{
				num6 -= Math.PI*2;
			}
			if (num7 > Math.PI*2)
			{
				num7 -= Math.PI*2;
			}
			if (num6 > num7)
			{
				num7 += Math.PI*2;
			}
			Curve result;
			try
			{
				result = Arc.Create(center, radius, num6, num7, arc.XDirection, arc.YDirection);
			}
			catch (Exception ex2)
			{
				string message2 = ex2.Message;
				MessageBox.Show("GetTwoCurvesCrossSeg 创建圆弧 " + ex2.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = null;
			}
			return result;
		}

		private bool IsCurveCrossWall(Curve curve, Element ele)
		{
			Wall wall = ele as Wall;
			if (wall == null)
			{
				return false;
			}
			double width = wall.WallType.Width;
			Curve curve2 = (wall.Location as LocationCurve).Curve;
			Line line = curve as Line;
			Line line2 = curve2 as Line;
			if (line != null && line2 == null)
			{
				return false;
			}
			if (line == null && line2 != null)
			{
				return false;
			}
			if (!CurveAssistFunc.IsParallel(curve, curve2))
			{
				return false;
			}
			if (this.GetTwoCurvesCrossSeg(curve2, curve) != null)
			{
				return true;
			}
			XYZ pt;
			if (line != null)
			{
				pt = Geometry.CalculatMidPoint(line);
				Geometry.CalculatMidPoint(line2);
			}
			else
			{
				pt = Geometry.CalculatMidPoint(curve as Arc);
				Geometry.CalculatMidPoint(curve2 as Arc);
			}
			double num = width / 2.0;
			Curve curve3 = this.CreateOffsetCurve(curve2, num);
			Curve curve4 = this.CreateOffsetCurve(curve2, -num);
			Line item = Line.CreateBound(curve3.GetEndPoint(0), curve4.GetEndPoint(0));
			Line item2 = Line.CreateBound(curve3.GetEndPoint(1), curve4.GetEndPoint(1));
			List<Curve> list = new List<Curve>();
			list.Add(curve3);
			list.Add(curve4);
			list.Add(item);
			list.Add(item2);
			XYZ hyendPoint = curve.GetEndPoint(0);
			XYZ hyendPoint2 = curve.GetEndPoint(1);
			bool flag = DlgFireOutlineWinDoorWallCheck.IsPointInArea(list, hyendPoint);
			bool flag2 = DlgFireOutlineWinDoorWallCheck.IsPointInArea(list, hyendPoint2);
			return flag || flag2 || DlgFireOutlineWinDoorWallCheck.IsPointInArea(list, pt);
		}

		private Curve CreateOffsetCurve(Curve curve, double dOffsetDist)
		{
			Line line = curve as Line;
			if (line != null)
			{
				XYZ xyz = line.Direction.CrossProduct(XYZ.BasisZ);
				xyz = xyz.Normalize() * dOffsetDist;
				return Line.CreateBound(line.GetEndPoint(0) + xyz, line.GetEndPoint(1) + xyz);
			}
			Arc arc = curve as Arc;
			if (arc == null)
			{
				return null;
			}
			double num = arc.Radius + dOffsetDist;
			Plane plane = ArcAssistFunc.GetPlane(arc);
			double startAngle = ArcAssistFunc.GetStartAngle(arc);
			double num2 = ArcAssistFunc.GetEndAngle(arc);
			if (startAngle > num2)
			{
				num2 += Math.PI*2;
			}
			return Arc.Create(plane, num, startAngle, num2);
		}

		public static bool IsPointInArea(List<Curve> area, XYZ pt)
		{
			List<XYZ> list = new List<XYZ>();
			foreach (Curve curve in area)
			{
				list.AddRange(curve.Tessellate());
			}
			return Geometry.PointInPloygon(list, pt) == 1;
		}

		private ModelessFromRequestHandler m_Handler;

		private ExternalEvent m_ExEvent;

		private ExternalCommandData m_Revit;

		private List<ViewPlan> m_lstViewPlan = new List<ViewPlan>();

		private static int g_Dlg_X = 10;

		private static int g_Dlg_Y = 10;

        public WindowHandle m_wndHandleRevit;

		private UIDocument m_UIDoc;

		public List<DlgFireOutlineWinDoorWallCheck.Def_Sel> m_lstDefine = new List<DlgFireOutlineWinDoorWallCheck.Def_Sel>();

		public string m_strDoorClassAFiName;

		public string m_strDoorSuperFiName;

		public string m_strWindowFiName;

		public List<DbFamily> m_lstFamilyLibDoors;

		public List<DbFamily> m_lstFamilyLibDoorsRolling;

		public List<DbFamily> m_lstFamilyLibWins;

		public enum EleType
		{
			eWall,
			eDoor,
			eWindow,
			eDoorRolling
		}

		public class Def_Sel
		{
			public bool IsSingleDoor()
			{
				return this.m_kType == DlgFireOutlineWinDoorWallCheck.EleType.eDoor && DlgFireOutlineWinDoorWallCheck.Def_Sel.IsSingleDoor((int)AssistFunc.feetToMM((this.m_element as FamilyInstance).Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsDouble()));
			}

			public static bool IsSingleDoor(int nWide)
			{
				return nWide <= 1000;
			}

			public string GetNewSymbolName()
			{
				if (this.m_kType == DlgFireOutlineWinDoorWallCheck.EleType.eWall)
				{
					return "";
				}
				string result = "";
				FamilyInstance familyInstance = this.m_element as FamilyInstance;
				Parameter parameter = familyInstance.Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH);
				Parameter parameter2 = familyInstance.Symbol.GetParameter(BuiltInParameter.FAMILY_HEIGHT_PARAM);
				int num = (int)(AssistFunc.feetToMM(parameter.AsDouble()) / 100.0);
				int num2 = (int)(AssistFunc.feetToMM(parameter2.AsDouble()) / 100.0);
				switch (this.m_kType)
				{
				case DlgFireOutlineWinDoorWallCheck.EleType.eDoor:
					result = "FM" + num.ToString("D2") + num2.ToString("D2") + " 甲";
					break;
				case DlgFireOutlineWinDoorWallCheck.EleType.eWindow:
					result = "FC" + num.ToString("D2") + num2.ToString("D2") + " 甲";
					break;
				case DlgFireOutlineWinDoorWallCheck.EleType.eDoorRolling:
					result = "FJLM" + num.ToString("D2") + num2.ToString("D2") + " 特";
					break;
				}
				return result;
			}

			public Element m_element;

			public string m_strEleName;

			public DlgFireOutlineWinDoorWallCheck.EleType m_kType;

			public ElementId m_viewId;

			public string m_strViewName;
		}
	}
}
